package com.jcw.kqxt.controller;

import com.jcw.kqxt.customAnnotation.PermissionVerification;
import com.jcw.kqxt.dto.AttendanceDto;
import com.jcw.kqxt.query.AttendanceQuery;
import com.jcw.kqxt.result.JSONResult;
import com.jcw.kqxt.result.PageList;
import com.jcw.kqxt.service.IAttendanceService;
import com.jcw.kqxt.domain.Attendance;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.spring.web.json.Json;

import java.util.List;

/**
 * @desc
 * @since 2022-02-25
 */
@RestController
@RequestMapping("/attendance")
public class AttendanceController {

    @Autowired
    public IAttendanceService attendanceService;

    /**
     * 修改
     * @param attendance 前端传递来的实体数据
     */
    @PostMapping(value="/update")
    @PermissionVerification(auth = "attendance:update")
    @Transactional
    public JSONResult update(@RequestBody Attendance attendance){
        attendanceService.updateById(attendance);
        return JSONResult.success();
    }
    /**
     * 保存
     * @param attendance 前端传递来的实体数据
     */
    @PostMapping(value="/save")
    @Transactional
    @PermissionVerification(auth = "attendance:save")
    public JSONResult save(@RequestBody Attendance attendance){
        attendanceService.insert(attendance);
        return JSONResult.success();
    }

    /**
    * 根据ID删除指定对象信息
    * @param id
    */
    @DeleteMapping(value="/{id}")
    @Transactional
    @PermissionVerification(auth = "attendance:delete:id")
    public JSONResult delete(@PathVariable("id") Long id){
            attendanceService.deleteById(id);
        return JSONResult.success();
    }

    //根据ID查询对象详情信息
    @GetMapping(value = "/{id}")
    @PermissionVerification(auth = "attendance:select:id")
    public JSONResult get(@PathVariable("id")Long id)
    {
        return JSONResult.success(attendanceService.selectById(id));
    }


    /**
    * 查看所有对象数据（不分页）
    */
    @GetMapping(value = "/list")
    @PermissionVerification(auth = "attendance:list")
    public JSONResult list(){
        return JSONResult.success(attendanceService.selectList(null));
    }


    /**
    * 分页查询数据
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping(value = "/pagelist")
    @PermissionVerification(auth = "attendance:pagelist")
    public JSONResult pageList(@RequestBody AttendanceQuery query)
    {
        Page<Attendance> page = new Page<Attendance>(query.getPage(),query.getRows());
        page = attendanceService.selectPage(page);
        return JSONResult.success(new PageList<Attendance>(page.getTotal(), page.getRecords()));
    }

    /**
     * 考勤查询
     */
    @GetMapping("/find")
    @PermissionVerification(auth = "attendance:find")
    public JSONResult findAttendanceByDto(@RequestBody AttendanceDto dto){
        return attendanceService.findAttendanceByDto(dto);
    }
    /**
     * 考勤查询所有
     */
    @GetMapping("/findAll")
    @PermissionVerification(auth = "attendance:findAll")
    public JSONResult findAll(@RequestBody AttendanceDto dto){
        return attendanceService.findAll(dto);
    }
    /**
     * todo 权限没加 一键考勤/派遣...
     */
    @PostMapping("/oneKeyOperation")
    @PermissionVerification(auth = "attendance:oneKeyOperation")
    public JSONResult oneKeyOperation(@RequestBody AttendanceDto dto){
        return attendanceService.oneKeyOperation(dto);
    }

}